package com.base.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lazyzhao.util.pagination.Pagination;


public class DevicesService extends BasicService{
	
	public static final String TABLENAME_DEVICES = "devices";
	
	public static final String TABLENAME_APPS = "apps";
	
	
	public static final String TABLENAME_DEVICES_LIST = "(select a.*,c.accessstrategy  from devices a left join apps c on a.apps=c.id) Z";
	public String TABLENAME_DEVICES_VIEW = "(select d.* from devices d) Z";
	public Pagination<List<Map<String,Object>>> findDevices(Connection conn,Map<String,Object> params,Pagination<List<Map<String,Object>>> pagination) throws SQLException{
		return query(conn, tname(TABLENAME_DEVICES), params, pagination);
	}
	

	public Map<String,Object> findAppById(Connection conn,String id) throws SQLException{
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		return queryMap(conn, TABLENAME_APPS, params);
	}
	public Pagination<List<Map<String,Object>>> findDevices2(Connection conn,Map<String,Object> params,Pagination<List<Map<String,Object>>> pagination) throws SQLException{
		return query(conn, tname(TABLENAME_DEVICES_LIST), params, pagination);
	}
	public List<Map<String,Object>> findDevices(Connection conn,Map<String,Object> params,int... startend) throws SQLException{
		int start =0;
		int end = 999999999;
		
		if(startend.length>0){
			start = startend[0];
		}
		if(startend.length>1){
			end = startend[1];
		}
		
		return queryMapList(conn, tname(TABLENAME_DEVICES), params, start, end);
	}
	
	public Pagination<List<Map<String,Object>>> findDevicesView(Connection conn,Map<String,Object> params,Pagination<List<Map<String,Object>>> pagination) throws SQLException{
		return query(conn, TABLENAME_DEVICES_VIEW, params, pagination);
	}

	public Map<String,Object> findDeviceById(Connection conn,Map<String,Object> params) throws SQLException{
		return queryMap(conn, TABLENAME_DEVICES_LIST, params);
	}
	public Map<String,Object> findDeviceById(Connection conn,String id) throws SQLException{
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		return queryMap(conn, TABLENAME_DEVICES_VIEW, params);
	}
	
	public int insertDevices(Connection conn,Map<String,Object> objects) throws SQLException{
		return query.insert(conn, tname(TABLENAME_DEVICES), objects);
	}

	public int updateDevice(Connection conn,Map<String,Object> objects, String... wheres) throws SQLException{
		return query.update(conn, tname(TABLENAME_DEVICES), objects,wheres);
	}
	public int updateDevices(Connection conn,List<Map<String,Object>> objects, String... wheres) throws SQLException{
		return query.update(conn, tname(TABLENAME_DEVICES), objects,wheres);
	}

	public int deleteDevices(Connection conn,String[] ids) throws SQLException{
		Object[][] objs = new Object[ids.length][];
		for(int i=0;i<ids.length;i++){
			objs[i] = new Object[]{ids[i]}; 
		}
		int[] ints = query.batch(conn, "delete from " + tname(TABLENAME_DEVICES) + " where id=?",objs);
		int count = 0;
		for(int i : ints){
			count+=i;
		}
		return count;
	}
}
